home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
xlib
/
tdepth.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
9KB
|
329 lines
/*
* (c) Copyright 1993-94, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/keysym.h>
static int RGBattributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
None,
};
static int CIattributes[] = {
GLX_DEPTH_SIZE, 1,
None,
};
enum {
BLACK = 0,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE
};
#define COLOR_OFFSET_1 16
#define COLOR_OFFSET_2 32
static float rgbMap[][3] = {
{0, 0, 0},
{1, 0, 0},
{0, 1, 0},
{1, 1, 0},
{0, 0, 1},
{1, 0, 1},
{0, 1, 1},
{1, 1, 1}
};
static long W = 300, H = 300;
static long size, mode, rgb;
static long antiAlias = 0;
static long stipple = 0;
static long ci1 = BLUE, ci2 = GREEN;
static unsigned char stippleBits[32*4] = {
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
};
static void DoDisplay(void)
{
glEnable(GL_SCISSOR_TEST);
glEnable(GL_DEPTH_TEST);
glViewport(10, 10, W-20, H-20);
glScissor(10, 10, W-20, H-20);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if (antiAlias) {
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
} else {
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
}
if (stipple) {
glPolygonStipple(stippleBits);
glEnable(GL_POLYGON_STIPPLE);
} else {
glDisable(GL_POLYGON_STIPPLE);
}
/*
** Draw two triangles that intersect each other.
*/
glBegin(GL_TRIANGLES);
rgb ? glColor3fv(rgbMap[BLUE]) : glIndexi(ci1);
glVertex3f( 0.9, -0.9, -30.0);
glVertex3f( 0.9, 0.9, -30.0);
glVertex3f(-0.9, 0.0, -30.0);
rgb ? glColor3fv(rgbMap[GREEN]) : glIndexi(ci2);
glVertex3f(-0.9, -0.9, -40.0);
glVertex3f(-0.9, 0.9, -40.0);
glVertex3f( 0.9, 0.0, -25.0);
glEnd();
glFlush();
}
static void Usage(void)
{
printf("Usage: tdepth [-c]\n");
printf(" -c: Run in color index mode\n");
exit(-1);
}
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int main(long argc, char** argv)
{
XVisualInfo *vi;
Display *dpy;
Colormap cmap;
Window window;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needDisplay;
int i;
rgb = 1;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'c':
rgb = 0;
break;
default:
Usage();
}
} else {
Usage();
}
}
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
return -1;
}
vi = glXChooseVisual(dpy, DefaultScreen(dpy),
rgb ? RGBattributes : CIattributes);
if (!vi) {
fprintf(stderr, "No appropriate visual on \"%s\"\n",
getenv("DISPLAY"));
return -1;
}
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
rgb ? AllocNone : AllocAll);
swa.border_pixel = 0;
swa.colormap = cmap;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
| KeyReleaseMask;
window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
W, H,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa);
XSetWMColormapWindows(dpy, window, &window, 1);
XMapWindow(dpy, window);
XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can't make window current to context\n");
return -1;
}
if (!rgb) {
XColor xc[32+2];
/* Build two color ramps to use */
unsigned long pixel;
unsigned short red, green, blue;
char flags; /* do_red, do_green, do_blue */
char pad;
for (i = 0; i < 16; i++) {
xc[i].pixel = i + COLOR_OFFSET_1;
xc[i].red = 0;
xc[i].green = 0;
xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
xc[i].flags = DoRed | DoGreen | DoBlue;
xc[i+16].pixel = i + COLOR_OFFSET_2;
xc[i+16].red = 0;
xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
xc[i+16].blue = 0;
xc[i+16].flags = DoRed | DoGreen | DoBlue;
}
xc[32].pixel = BLUE;
xc[32].red = xc[32].green = 0;
xc[32].blue = 65535;
xc[32].flags = DoRed|DoGreen|DoBlue;
xc[32].pixel = GREEN;
xc[32].red = xc[32].blue = 0;
xc[32].green = 65535;
xc[32].flags = DoRed|DoGreen|DoBlue;
XStoreColors(dpy, cmap, xc, 34);
}
mode = 0;
size = 1;
needDisplay = GL_TRUE;
for (;;) {
do {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
needDisplay = GL_TRUE;
break;
case ConfigureNotify:
W = event.xconfigure.width;
H = event.xconfigure.height;
needDisplay = GL_TRUE;
break;
case KeyPress:
{
char buf[100];
int rv;
KeySym ks;
rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
switch (ks) {
case XK_A:
case XK_a:
antiAlias = !antiAlias;
if (!rgb) {
if (antiAlias) {
ci1 = COLOR_OFFSET_1;
ci2 = COLOR_OFFSET_2;
} else {
ci1 = BLUE;
ci2 = GREEN;
}
}
needDisplay = GL_TRUE;
break;
case XK_S:
case XK_s:
stipple = !stipple;
needDisplay = GL_TRUE;
break;
case XK_Escape:
return 0;
}
}
break;
}
} while (XPending(dpy) != 0);
if (needDisplay) {
needDisplay = GL_FALSE;
DoDisplay();
}
}
}